package com.wanmait.baohan.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.wanmait.baohan.mapper.BookingTicketApplicationMapper;
import com.wanmait.baohan.mapper.TicketApplicationDetailMapper;

import com.wanmait.baohan.pojo.BookingTicketApplication;

import com.wanmait.baohan.service.BookingTicketApplicationService;
import com.wanmait.baohan.util.CreatJobNumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookingTicketApplicationServiceImpl implements BookingTicketApplicationService {
    @Resource
    private BookingTicketApplicationMapper bookingTicketApplicationMapper;

    @Resource
    private TicketApplicationDetailMapper ticketApplicationDetailMapper;

    @Resource
    private HttpServletRequest request;

    @Override
    public void submitTicketApplication(BookingTicketApplication bookingTicketApplication) {
        //提交申请
        bookingTicketApplication.setAddTime(new Date());
        bookingTicketApplication.setState("1");
        bookingTicketApplication.setNumbering("ticket" + CreatJobNumberUtil.creatJobNumber((Integer) request.getServletContext().getAttribute("ticketApplicationNumbering")));
        bookingTicketApplicationMapper.insert(bookingTicketApplication);
        System.out.println(bookingTicketApplication.getId());
        //提交详情
        bookingTicketApplication.getTicketApplicationDetails().forEach(
            ticketApplicationDetail -> ticketApplicationDetail.setBookingTicketApplicationId(bookingTicketApplication.getId()));
        ticketApplicationDetailMapper.insertList(bookingTicketApplication.getTicketApplicationDetails());
    }

    @Override
    public PageInfo<BookingTicketApplication> selectMyTicketApplications(Integer page, Integer limit, BookingTicketApplication bookingTicketApplication) {
        PageHelper.startPage(page, limit);
        List<BookingTicketApplication> BookingTicketApplications = bookingTicketApplicationMapper.selectByStaffid(bookingTicketApplication);
        PageInfo<BookingTicketApplication> pageInfo = new PageInfo<>(BookingTicketApplications, 7);
        return pageInfo;
    }

    @Override
    public BookingTicketApplication findById(Integer id) {
        return bookingTicketApplicationMapper.selectByPrimaryKey(id);
    }

    /**
     * hxn
     * @param page
     * @param limit
     * @param bookingTicketApplication
     * @return
     * 获取所有的订票申请，有条件
     */
    @Override
    public PageInfo<BookingTicketApplication> findAllWithPageAndLimit(Integer page, Integer limit, BookingTicketApplication bookingTicketApplication) {
        PageHelper.startPage(page, limit);
        List<BookingTicketApplication> bookingTicketApplications = bookingTicketApplicationMapper.selectAll(bookingTicketApplication);
        PageInfo<BookingTicketApplication> pageInfo = new PageInfo<>(bookingTicketApplications, 7);
        return pageInfo;
    }

    @Override
    public BookingTicketApplication selectByReId(Integer id) {
        return bookingTicketApplicationMapper.selectByReId(id);
    }

    @Override
    public void updateByIdState(Integer id,Integer state) {
        bookingTicketApplicationMapper.updateState(id,state);
    }

    @Override
    public Integer selectNode(Integer id) {
        return bookingTicketApplicationMapper.selectNode(id);
    }

    @Override
    public BookingTicketApplication findByTicket(BookingTicketApplication bookingTicketApplication) {
        return bookingTicketApplicationMapper.selectByTicket(bookingTicketApplication);
    }
}
